package com.jeecg.demo.controller;

import com.jeecg.demo.entity.MtCoreTiaokEntity;
import com.jeecg.demo.entity.MtSlHetEntity;
import com.jeecg.demo.entity.SysAppMessagesenderEntity;
import com.jeecg.demo.entity.SysAttachmentEntity;
import com.jeecg.demo.entity.VEcpHetslinfoEntity;
import com.jeecg.demo.service.MtCoreTiaokServiceI;
import com.jeecg.demo.service.MtSlHetServiceI;
import com.jeecg.demo.service.SysAppMessagesenderServiceI;
import com.jeecg.demo.service.SysAttachmentServiceI;
import com.jeecg.demo.service.VEcpHetslinfoServiceI;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"/vEcpHetslStatisinfoController"})
public class VEcpHetslStatisinfoController
  extends BaseController
{
  private static final Logger logger = LoggerFactory.getLogger(VEcpHetslStatisinfoController.class);
  @Autowired
  private VEcpHetslinfoServiceI vEcpHetslinfoService;
  @Autowired
  private SystemService systemService;
  @Autowired
  private MtCoreTiaokServiceI mtCoreTiaokService;
  @Autowired
  private SysAttachmentServiceI sysAttachmentService;
  @Autowired
  private MtSlHetServiceI mtHetService;
  @Autowired
  private SysAppMessagesenderServiceI sysAppMessagesender;
  
  @RequestMapping(params={"list"})
  public ModelAndView list(HttpServletRequest request, String type)
  {
    request.setAttribute("type", type);
    return new ModelAndView("com/jeecg/demo/vEcpHetslstatisinfoList");
  }
  
  @RequestMapping(params={"datagrid"})
  public void datagrid(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, String type)
  {
    CriteriaQuery cq = new CriteriaQuery(VEcpHetslinfoEntity.class, dataGrid);
    HqlGenerateUtil.installHql(cq, vEcpHetslinfo, request.getParameterMap());
    String ctBegin = request.getParameter("createTime_begin");
    String ctEnd = request.getParameter("createTime_end");
    if ("query".equals(type))
    {
      cq.eq("zhuangt", "4");
      cq.eq("dongzbm", "32");
      cq.add();
    }
    if ("queryzq".equals(type))
    {
      cq.eq("zhuangt", "4");
      cq.eq("dongzbm", "33");
      cq.add();
    }
    this.vEcpHetslinfoService.getDataGridReturn(cq, true);
    String chumdwmc = "";
    String maisdwmc = "";
    if (vEcpHetslinfo.getChumdwmc() != null) {
      chumdwmc = vEcpHetslinfo.getChumdwmc();
    }
    if (vEcpHetslinfo.getMaisdwmc() != null) {
      maisdwmc = vEcpHetslinfo.getMaisdwmc();
    }
    String total = "";
    String price = "";
    String totalprice = "";
    if ("query".equals(type))
    {
      total = 
        String.valueOf(this.vEcpHetslinfoService.findOneForJdbc(
        "select round(sum(shul)/10000,2) as ssum from  V_ECP_HETSLINFO where zhuangt = '4' and DONGZBM = '32' AND CHUMDWMC like '%" +
        chumdwmc + 
        "%' AND MAISDWMC like '%" + 
        maisdwmc + "%'", new Object[0]).get("ssum"));
     /* price =
        String.valueOf(this.vEcpHetslinfoService
        .findOneForJdbc(
        "select sum(jiag) as price from  V_ECP_HETSLINFO  where zhuangt = '4' and DONGZBM <> '32' AND CHUMDWMC like '%" + 
        chumdwmc + 
        "%' AND MAISDWMC like '%" + 
        maisdwmc + "%'", new Object[0]).get("price"));*/
      totalprice =  String.valueOf(this.vEcpHetslinfoService
              .findOneForJdbc(
                      "select round(sum(nvl(jiag,0) * shul)/10000,2) as totalprice from  V_ECP_HETSLINFO  where zhuangt = '4' and DONGZBM = '32' AND CHUMDWMC like '%" +
                              chumdwmc +
                              "%' AND MAISDWMC like '%" +
                              maisdwmc + "%'", new Object[0])
              .get("totalprice"));
    }
    else
    {
      total = 
        String.valueOf(this.vEcpHetslinfoService
        .findOneForJdbc(
        "select round(sum(shul)/10000,2) as ssum from  V_ECP_HETSLINFO where zhuangt = '4' and DONGZBM = '33' AND CHUMDWMC like '%" +
        chumdwmc + 
        "%' AND MAISDWMC like '%" + 
        maisdwmc + "%'", new Object[0]).get("ssum"));
      /*price =
        String.valueOf(this.vEcpHetslinfoService
        .findOneForJdbc(
        "select sum(jiag) as price from  V_ECP_HETSLINFO  where zhuangt = '4' and DONGZBM = '32' AND CHUMDWMC like '%" + 
        chumdwmc + 
        "%' AND MAISDWMC like '%" + 
        maisdwmc + "%'", new Object[0]).get("price"));*/
      totalprice =  String.valueOf(this.vEcpHetslinfoService
              .findOneForJdbc(
                      "select round(sum(nvl(jiag,0) * shul)/10000,2) as totalprice from  V_ECP_HETSLINFO  where zhuangt = '4' and DONGZBM = '33' AND CHUMDWMC like '%" +
                              chumdwmc +
                              "%' AND MAISDWMC like '%" +
                              maisdwmc + "%'", new Object[0])
              .get("totalprice"));
    }
    if ("query".equals(type))
    {
      String unit = "万吨";
      String unit1 = "万元";
      /*Double tt = Double.valueOf(total);
      //Double pp = Double.valueOf(price);
      //Double tp = Double.valueOf(tt.doubleValue() * pp.doubleValue());
      Double tp = Double.valueOf(totalprice);
      DecimalFormat df = new DecimalFormat("#.##");
      if (tt.doubleValue() >= 1000.0D)
      {
        unit = "吨";
        tt = tt;
      }
      if (((tp.doubleValue() >= 10000.0D ? 1 : 0) & (tp.doubleValue() < 1.0E8D ? 1 : 0)) != 0)
      {
        unit1 = "万元";
        tp = Double.valueOf(tp.doubleValue() / 10000.0D);
      }
      if (tp.doubleValue() >= 1.0E8D)
      {
        unit1 = "亿";
        tp = Double.valueOf(tp.doubleValue() / 1.0E8D);
      }
      dataGrid.setFooter("shouhdwmc:" + tt + unit +
        ",chumdwmc,chumdwmc:合计,qianysj:" + df.format(tp) + unit1 +
        ",htzt:" + "总金额");*/
      dataGrid.setFooter("shouhdwmc:" + total + unit +
              ",chumdwmc,chumdwmc:合计,qianysj:" + totalprice + unit1 +
              ",htzt:" + "总金额");
    }
    else
    {
      /*String unit = "吨";
      String unit1 = "元";
      Double tt = (double) 0;
      if(total != null && !total.equals("null")){
        tt = Double.valueOf(total);
      }
      Double pp = (double) 0;
      if(price != null && !price.equals("null")){
        pp = Double.valueOf(price);
      }
      Double tp = Double.valueOf(tt.doubleValue() * pp.doubleValue());
      DecimalFormat df = new DecimalFormat("#.##");
      if (tt.doubleValue() >= 1000.0D)
      {
        unit = "吨";
        tt = tt;
      }
      if (((tp.doubleValue() >= 10000.0D ? 1 : 0) & (tp.doubleValue() < 1.0E8D ? 1 : 0)) != 0)
      {
        unit1 = "万元";
        tp = Double.valueOf(tp.doubleValue() / 10000.0D);
      }
      if (tp.doubleValue() >= 1.0E8D)
      {
        unit1 = "亿";
        tp = Double.valueOf(tp.doubleValue() / 1.0E8D);
      }
      dataGrid.setFooter("shouhdwmc:" + tt + unit +
        ",chumdwmca:合计,qianysj:" + df.format(tp) + unit1 +
        ",htzt:" + "总金额");*/
      String unit = "万吨";
      String unit1 = "万元";
      /*Double tt = Double.valueOf(total);
      //Double pp = Double.valueOf(price);
      //Double tp = Double.valueOf(tt.doubleValue() * pp.doubleValue());
      Double tp = Double.valueOf(totalprice);
      DecimalFormat df = new DecimalFormat("#.##");
      if (tt.doubleValue() >= 1000.0D)
      {
        unit = "吨";
        tt = tt;
      }
      if (((tp.doubleValue() >= 10000.0D ? 1 : 0) & (tp.doubleValue() < 1.0E8D ? 1 : 0)) != 0)
      {
        unit1 = "万元";
        tp = Double.valueOf(tp.doubleValue() / 10000.0D);
      }
      if (tp.doubleValue() >= 1.0E8D)
      {
        unit1 = "亿";
        tp = Double.valueOf(tp.doubleValue() / 1.0E8D);
      }
      dataGrid.setFooter("shouhdwmc:" + tt + unit +
              ",chumdwmc,chumdwmc:合计,qianysj:" + df.format(tp) + unit1 +
              ",htzt:" + "总金额");*/
      dataGrid.setFooter("shouhdwmc:" + total + unit +
              ",chumdwmc,chumdwmc:合计,qianysj:" + totalprice + unit1 +
              ",htzt:" + "总金额");
    }
    TagUtil.datagrid(response, dataGrid);
  }
  
  @RequestMapping(params={"goConfirm"})
  public ModelAndView confirmPact(VEcpHetslinfoEntity vEcpHetglinfo, HttpServletRequest req, int flag)
  {
    if (StringUtil.isNotEmpty(vEcpHetglinfo.getId()))
    {
      vEcpHetglinfo = (VEcpHetslinfoEntity)this.vEcpHetslinfoService.getEntity(
        VEcpHetslinfoEntity.class, vEcpHetglinfo.getId());
      req.setAttribute("vEcpHetinfoPage", vEcpHetglinfo);
      req.setAttribute("confirmflag", Integer.valueOf(flag));
    }
    CriteriaQuery cq = new CriteriaQuery(MtCoreTiaokEntity.class);
    cq.add();
    
    List<MtCoreTiaokEntity> resultList = this.mtCoreTiaokService
      .getListByCriteriaQuery(cq, Boolean.valueOf(false));
    req.setAttribute("tiaoKList", resultList);
    CriteriaQuery cq1 = new CriteriaQuery(SysAttachmentEntity.class);
    cq1.eq("objid", Integer.valueOf(vEcpHetglinfo.getHetid()));
    cq1.addOrder("sysid", SortDirection.asc);
    cq1.add();
    
    String sql1 = "select t.FULLNAME FROM TRADER t WHERE t.TRADERID=?";
    List<Map<String, Object>> traderList = this.systemService.findForJdbc(sql1, new Object[] {
      vEcpHetglinfo.getChumdwbm() });
    String fullname = "";
    if (traderList.size() > 0) {
      fullname = (String)((Map)traderList.get(0)).get("FULLNAME");
    }
    req.setAttribute("fullname", fullname);
    
    String sql = "SELECT * from SYS_ATTACHMENT t WHERE t.OBJURL is not null and t.OBJID=? ORDER BY t.SYSID desc";
    List<Map<String, Object>> sysAttachList = this.systemService.findForJdbc(
      sql, new Object[] { vEcpHetglinfo.getSysid() });
    if (sysAttachList.size() > 3) {
      sysAttachList = sysAttachList.subList(0, 3);
    }
    req.setAttribute("sysAttachList", sysAttachList);
    req.setAttribute("prefixUrl", 
      ResourceUtil.getConfigByName("imageServerUrl"));
    return new ModelAndView("com/jeecg/demo/hetquerysl");
  }
  
  @RequestMapping(params={"doDel"})
  @ResponseBody
  public AjaxJson doDel(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    vEcpHetslinfo = (VEcpHetslinfoEntity)this.systemService.getEntity(VEcpHetslinfoEntity.class, 
      vEcpHetslinfo.getId());
    message = "V_ECP_HETSLINFO删除成功";
    try
    {
      this.vEcpHetslinfoService.delete(vEcpHetslinfo);
      this.systemService.addLog(message, Globals.Log_Type_DEL, 
        Globals.Log_Leavel_INFO);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message = "V_ECP_HETSLINFO删除失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }
  
  @RequestMapping(params={"doConfirm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public AjaxJson doConfirm(VEcpHetslinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "更行失败";
    
    CriteriaQuery cq = new CriteriaQuery(MtSlHetEntity.class);
    cq.eq("hetid", vEcpHetinfo.getHetid());
    cq.add();
    
    List<MtSlHetEntity> mtList = this.mtHetService.getListByCriteriaQuery(cq, 
      Boolean.valueOf(false));
    MtSlHetEntity mt = null;
    if (mtList.size() > 0)
    {
      mt = (MtSlHetEntity)mtList.get(0);
      try
      {
        mt.setZhuangt("4");
        this.mtHetService.save(mt);
        
        String url = "http://119.3.218.213:8088/jeecg-boot/api/contront/saveHetslinfo";
        vEcpHetinfo = 
        
          (VEcpHetslinfoEntity)this.vEcpHetslinfoService.findByQueryString("from VEcpHetslinfoEntity v where v.hetid = '" + vEcpHetinfo.getHetid() + "'").get(0);
        RestTemplate template = new RestTemplate();
        LinkedMultiValueMap params = new LinkedMultiValueMap();
        JSONObject jsmt = JSONObject.fromObject(vEcpHetinfo);
        System.out.print(jsmt.toString());
        params.add("json", jsmt.toString());
        HttpHeaders headers = new HttpHeaders();
        MediaType type = 
          MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<LinkedMultiValueMap> req = new HttpEntity(
          params, headers);
        
        String ret = "";
        ret = (String)template.postForObject(url, req, String.class, new Object[0]);
        
        this.systemService.addLog(message, Globals.Log_Type_UPDATE, 
          Globals.Log_Leavel_INFO);
        message = "更新成功";
        
        SysAppMessagesenderEntity appmessage = new SysAppMessagesenderEntity();
        appmessage.setSender(Integer.valueOf(0));
        appmessage.setReceiver(Integer.valueOf(vEcpHetinfo.getHetid()));
        appmessage.setOrderid(Integer.valueOf(1));
        appmessage.setMsgstatus("1");
        appmessage.setIsvalid("1");
        appmessage.setAppid("1");
        appmessage.setSendtime(new Date());
        appmessage.setFixedtime(new Date());
        
        String msg = "您的合同" + mt.getHetbh() + "已通过,您可以到合同列表中查看";
        appmessage.setMsgtitle(msg);
        appmessage.setMsgcontent(msg);
        this.sysAppMessagesender.save(appmessage);
      }
      catch (Exception e)
      {
        e.printStackTrace();
        message = "更新失败";
        throw new BusinessException(e.getMessage());
      }
    }
    j.setMsg(message);
    return j;
  }
  
  @RequestMapping(params={"doBatchDel"})
  @ResponseBody
  public AjaxJson doBatchDel(String ids, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "V_ECP_HETSLINFO删除成功";
    try
    {
      String[] arrayOfString;
      int j1 = (arrayOfString = ids.split(",")).length;
      for (int i = 0; i < j1; i++)
      {
        String id = arrayOfString[i];
        VEcpHetslinfoEntity vEcpHetslinfo = (VEcpHetslinfoEntity)this.systemService.getEntity(
          VEcpHetslinfoEntity.class, Integer.valueOf(Integer.parseInt(id)));
        this.vEcpHetslinfoService.delete(vEcpHetslinfo);
        this.systemService.addLog(message, Globals.Log_Type_DEL, 
          Globals.Log_Leavel_INFO);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message = "V_ECP_HETSLINFO删除失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }
  
  @RequestMapping(params={"doAdd"})
  @ResponseBody
  public AjaxJson doAdd(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "V_ECP_HETSLINFO添加成功";
    try
    {
      this.vEcpHetslinfoService.save(vEcpHetslinfo);
      this.systemService.addLog(message, Globals.Log_Type_INSERT, 
        Globals.Log_Leavel_INFO);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message = "V_ECP_HETSLINFO添加失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }
  
  @RequestMapping(params={"doUpdate"})
  @ResponseBody
  public AjaxJson doUpdate(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "V_ECP_HETSLINFO更新成功";
    VEcpHetslinfoEntity t = (VEcpHetslinfoEntity)this.vEcpHetslinfoService.get(
      VEcpHetslinfoEntity.class, vEcpHetslinfo.getId());
    try
    {
      MyBeanUtils.copyBeanNotNull2Bean(vEcpHetslinfo, t);
      this.vEcpHetslinfoService.saveOrUpdate(t);
      this.systemService.addLog(message, Globals.Log_Type_UPDATE, 
        Globals.Log_Leavel_INFO);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message = "V_ECP_HETSLINFO更新失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }
  
  @RequestMapping(params={"goAdd"})
  public ModelAndView goAdd(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest req)
  {
    if (StringUtil.isNotEmpty(vEcpHetslinfo.getId()))
    {
      vEcpHetslinfo = (VEcpHetslinfoEntity)this.vEcpHetslinfoService.getEntity(
        VEcpHetslinfoEntity.class, vEcpHetslinfo.getId());
      req.setAttribute("vEcpHetslinfo", vEcpHetslinfo);
    }
    return new ModelAndView("com/jeecg/demo/vEcpHetslinfo-add");
  }
  
  @RequestMapping(params={"goUpdate"})
  public ModelAndView goUpdate(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest req)
  {
    if (StringUtil.isNotEmpty(vEcpHetslinfo.getId()))
    {
      vEcpHetslinfo = (VEcpHetslinfoEntity)this.vEcpHetslinfoService.getEntity(
        VEcpHetslinfoEntity.class, vEcpHetslinfo.getId());
      req.setAttribute("vEcpHetslinfo", vEcpHetslinfo);
    }
    return new ModelAndView("com/jeecg/demo/vEcpHetslinfo-update");
  }
  
  @RequestMapping(params={"upload"})
  public ModelAndView upload(HttpServletRequest req)
  {
    req.setAttribute("controller_name", "vEcpHetslinfoController");
    return new ModelAndView("common/upload/pub_excel_upload");
  }
  
  @RequestMapping(params={"exportXls"})
  public String exportXls(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, ModelMap modelMap)
  {
    CriteriaQuery cq = new CriteriaQuery(VEcpHetslinfoEntity.class, 
      dataGrid);
    HqlGenerateUtil.installHql(cq, 
      vEcpHetslinfo, request.getParameterMap());
    List<VEcpHetslinfoEntity> vEcpHetslinfos = this.vEcpHetslinfoService
      .getListByCriteriaQuery(cq, Boolean.valueOf(false));
    modelMap.put("fileName", "V_ECP_HETSLINFO");
    modelMap.put("entity", VEcpHetslinfoEntity.class);
    modelMap.put("params", new ExportParams(
      "V_ECP_HETSLINFO列表", "导出人:" +
      ResourceUtil.getSessionUser().getRealName(), "导出信息"));
    modelMap.put("data", vEcpHetslinfos);
    return "jeecgExcelView";
  }
  
  @RequestMapping(params={"exportXlsByT"})
  public String exportXlsByT(VEcpHetslinfoEntity vEcpHetslinfo, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, ModelMap modelMap)
  {
    modelMap.put("fileName", "V_ECP_HETSLINFO");
    modelMap.put("entity", VEcpHetslinfoEntity.class);
    modelMap.put("params", new ExportParams(
      "V_ECP_HETSLINFO列表", "导出人:" +
      ResourceUtil.getSessionUser().getRealName(), "导出信息"));
    modelMap.put("data", new ArrayList());
    return "jeecgExcelView";
  }
  
  
  
  @RequestMapping(params={"doBack"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public AjaxJson doBack(VEcpHetslinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "退回失败";
    
    CriteriaQuery cq = new CriteriaQuery(MtSlHetEntity.class);
    cq.eq("hetid", vEcpHetinfo.getHetid());
    cq.add();
    
    List<MtSlHetEntity> mtList = this.mtHetService.getListByCriteriaQuery(cq, 
      Boolean.valueOf(false));
    MtSlHetEntity mt = null;
    if (mtList.size() > 0)
    {
      mt = (MtSlHetEntity)mtList.get(0);
      try
      {
        mt.setZhuangt("1");
        this.mtHetService.save(mt);
        this.systemService.addLog(message, Globals.Log_Type_UPDATE, 
          Globals.Log_Leavel_INFO);
        message = "退回成功";
      }
      catch (Exception e)
      {
        e.printStackTrace();
        message = "退回失败";
        throw new BusinessException(e.getMessage());
      }
    }
    j.setMsg(message);
    return j;
  }
}
